package com.rede.didiok.rank.web;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.rank.RankDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;
import com.rede.didiok.rank.entity.RankEntity;
import com.rede.didiok.rank.service.RankService;
import com.rede.didiok.rank.vo.RankVo;


/**
 * 榜单，榜单的主体信息,一个榜单对应一条记录，一个榜单包含多个 rank_rank_member 记录
 *
 * @author Rede
 * @email didiok@foxmail.com
 * @date 2023-07-20 22:09:24
 */
@Slf4j
@RestController
@RequestMapping("rank/feign/rank")
public class RankController {
    @Autowired
    private RankService rankService;

    /**
     * 根据分类获取榜单列表
     * /product/categorybrandrelation/brands/list
     */
    @GetMapping("/listbycategory")
    public R listRankByCategory(@RequestParam(value = "catUid",required = true) String catUid){
        List<RankVo> rankVoList = rankService.listRankByCategory(catUid);

        return R.ok().put("data", rankVoList);
    }

    @ApiOperation(value = "获取榜单列表", notes = "获取榜单列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取榜单列表");
        return ResultUtil.successWithData(rankService.getPageList(rankDto));
    }

    @AvoidRepeatableCommit
    @ApiOperation(value = "增加榜单", notes = "增加榜单", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加榜单");
        return rankService.addRank(rankDto);
    }

    @ApiOperation(value = "编辑榜单", notes = "编辑榜单", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody RankDto rankDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑榜单");
        return rankService.editRank(rankDto);
    }

    @ApiOperation(value = "批量删除榜单", notes = "批量删除榜单", response = String.class)
    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<RankDto> rankDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("批量删除榜单");
        return rankService.deleteBatchRank(rankDtoList);
    }

    @ApiOperation(value = "置顶榜单", notes = "置顶榜单", response = String.class)
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody RankDto rankDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶榜单");
        return rankService.stickRank(rankDto);
    }

    /**
     * 通过引用量排序榜单
     * 引用量就是所有的文章中，有多少使用了该榜单，如果使用的越多，该榜单的引用量越大，那么排名越靠前
     *
     * @return
     */
    @ApiOperation(value = "通过引用量排序榜单", notes = "通过引用量排序榜单", response = String.class)
    @PostMapping("/rankSortByMemberCount")
    public String rankSortByMemberCount() {
        log.info("通过引用量排序榜单");
        return rankService.rankSortByMemberCount();
    }

/***********************分割线，我也不知道下面的有没有使用到***********/
    /**
     * 信息
     */
    @RequestMapping("/info/{rankId}")
    // @RequiresPermissions("rank:rank:info")
    public R info(@PathVariable("rankId") Long rankId){
		RankEntity rank = rankService.getById(rankId);

        return R.ok().put("rank", rank);
    }

    /**
     * 批量：根据榜单id列表，获取榜单信息
     * @param rankUids
     * @return
     */
    @GetMapping("/infos")
    public R infos(@RequestParam("rankIds") List<String> rankUids){
        List<RankEntity> rank =  rankService.getRanksByIds(rankUids);

        return R.ok().put("rank", rank);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("rank:rank:save")
    public R save(@Valid @RequestBody RankEntity rank){
        rankService.save(rank);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("rank:rank:update")
    public R update(@Valid @RequestBody RankEntity rank){
        rankService.updateById(rank);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/status")
    // @RequiresPermissions("product:brand:update")
    public R updateStatus(@RequestBody RankEntity rank){
        rankService.updateDeleteStatusByUid(rank);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("rank:rank:delete")
    public R delete(@RequestBody Long[] rankIds){
        rankService.removeByIds(Arrays.asList(rankIds));

        return R.ok();
    }

}
